/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef CONNECTION_MANAGER_H
#define CONNECTION_MANAGER_H

#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>

#include "i_socket.h"
#include "sender.h"
#include "listener.h"
#include "receiver.h"
#include "filetype.h"
#include "connection_listener.h"

#include <map>
#include <list>

using namespace std;

/** 
 * \brief This namespaces includes all nertwork related stuff.
 * \author Michael Opitz
 */
namespace network {
	/** 
	 * \brief This class is a facade to the whole network-functionality in
	 * this namespace.
	 */
	class connection_manager : public network::listener {
	public:
		/** 
		 * \brief connection_manager is the constructor of the
		 * connection_manager class. It is initialized with a
		 * message_queue.
		 * 
		 * \param q is a shared_ptr of a message_queue.
		 */
		connection_manager(boost::shared_ptr<message_queue> q)
		: _queue(q) {}
		~connection_manager();

		/** 
		 * \brief add_recv adds a new receiver to the network-system.
		 * 
		 * \param sock is the socket from which messages should be
		 * received.
		 * \param file is the filetype, which are received from sock
		 */
		void add_recv(boost::shared_ptr<network::i_socket> sock,
				boost::shared_ptr<network::filetype> file);
		/** 
		 * \brief add_listener adds a new tcp-connection_listener to the
		 * network-system.
		 * 
		 * \param port is the port at which the listener should listen
		 * 
		 * \return the created listener-socket is returned, which can be
		 * used in user_handlers.
		 */
		boost::shared_ptr<network::i_socket> add_listener(int port);
		/** 
		 * \brief get_sender finds the appropriate file_type for a
		 * socket. If you want to send something over a socket, you
		 * should use this method to get the appropriate file_type, to
		 * convert your dom-structure to a string.
		 * 
		 * \param sock is the socket, to which you want to send
		 * something
		 * 
		 * \return a shared_ptr of network::sender is returned.
		 */
		boost::shared_ptr<network::sender> get_sender(
			boost::shared_ptr<network::i_socket> sock);

		/** 
		 * \brief notify is called from receiver, which want to be
		 * removed from the internal collection.
		 * 
		 * \param n is a pointer to a network::receiver.
		 */
		virtual void notify(const network::notifier *n);
	private:
		list<boost::shared_ptr<network::receiver> > _receiver;
		list<boost::shared_ptr<network::connection_listener> > 
			_listener;

		map<	boost::shared_ptr<network::i_socket>, 
			boost::shared_ptr<network::sender> > _sender;

		boost::mutex _mutex;
		boost::shared_ptr<message_queue> _queue;
	};
}

#endif

